#include "comm.h"
#include <unistd.h>

int main()
{
  key_t key = ftok(PATH_NAME,PROJ_ID);
  if(key < 0) {
    perror("ftok");
    return 1;
  }
  
  //创建全新共享内存，如果和系统已经存在的ID冲突，出错返回
  int shmid = shmget(key,SIZE,IPC_CREAT|IPC_EXCL|0666);
  if(shmid < 0) {
    perror("shmget");
    return 2;
  }

  printf("key: %u,shmid: %d\n",key,shmid);
  //sleep(1);

  char *mem = (char*)shmat(shmid,NULL,0);
  printf("attaches shm success\n");
  //sleep(15);

  //这里就是通信逻辑
  while(1) {
    sleep(1);
    printf("%s\n",mem);
  }

  shmdt(mem);
  printf("detaches shm success\n");
  //sleep(5);

  shmctl(shmid,IPC_RMID,NULL);
  printf("key: 0x%x,shmid: %d -> shm delete success\n",key,shmid);

  //sleep(10);

  return 0;
}
